package com.example.spring_seatreservation.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.spring_seatreservation.Bean.R;
import com.example.spring_seatreservation.entity.Floor;
import com.example.spring_seatreservation.mapper.FloorMapper;
import com.example.spring_seatreservation.mapper.ReadingRoomMapper;
import com.example.spring_seatreservation.mapper.SeminarRoomMapper;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/floor")
@AllArgsConstructor
public class FloorController {

    private final FloorMapper floorMapper;
    private final ReadingRoomMapper readingRoomMapper;
    private final SeminarRoomMapper seminarRoomMapper;

    @GetMapping("getFloorList")
    public List<Floor> getFloorList(){
        return floorMapper.getFloorList();
    }

    @GetMapping("getFloorListByLibId")
    public List<Floor> getFloorListByLibId(String libId){
        LambdaQueryWrapper<Floor> lambdaQueryWrapper = Wrappers.<Floor>lambdaQuery().eq(Floor::getLibId,libId);
        return floorMapper.selectList(lambdaQueryWrapper);
    }

    @PostMapping("submit")
    public Map<String, Object> submit(@RequestBody Floor floor){
        if (floor.getId() == null) {
            floorMapper.insert(floor);
        } else {
            floorMapper.updateById(floor);
        }
        return new R().ok().builder();
    }

    @PostMapping("delFloor")
    public Map<String, Object> delFloor(@RequestBody Floor floor){
        int readRoomNum = readingRoomMapper.selectRoomByFloorId(floor.getId());
        int seminarRoomNum = seminarRoomMapper.selectRoomByFloorId(floor.getId());
        if (readRoomNum > 0) {
            return new R().bad("清先删除该楼层的阅览室").builder();
        }
        if (seminarRoomNum > 0) {
            return new R().bad("清先删除该楼层的研讨室").builder();
        }
        floorMapper.deleteById(floor.getId());
        return new R().ok().builder();
    }
}
